编写一个属于自己的DPDK应用程序(DPDK入门向)

您所在的位置:网站首页 百度 dpdk 编写一个属于自己的DPDK应用程序(DPDK入门向)

编写一个属于自己的DPDK应用程序(DPDK入门向)

2024-07-16 16:47| 来源: 网络整理| 查看: 265

编写一个属于自己的DPDK应用程序(DPDK入门向) 1.前言2.DPDK 的示例 examples3.helloworld4.以skeleton(basicfwd)为基础,编写自己的应用程序。5.依葫芦画瓢+抛砖引玉6.编译自己的应用程序7.总结

1.前言

最近这几周在研究dpdk,测试性能,因为是从零开始需要找各种各样的资料,然后发现要自己编写一个dpdk应用程序,真正对自己帮助最大的还是官方的文档,一个是api文档,一个是gudides文档(http://doc.dpdk.org/api https://doc.dpdk.org/guides)。毫无疑问的全是英文文档,基本需要借助网页翻译连猜带蒙。 作为程序员这应该是基本技能了,而我写这篇文章的目的,一是对自己这段时间的研究成果做一个积累,二是希望帮助大家更快的上手dpdk,编写一个dpdk应用程序。

2.DPDK 的示例 examples

dpdk的下载,安装和使用直接百度就有很多的教程,跟着教程来应该问题不大,这里主要提醒大家注意dpdk给出的示例

在这里插入图片描述 这里面有dpdk大部分的应用场景,想要详细了解可以去guides网页(前文有网址),像二层转发 三层转发(l2fwd l3fwd)网上很多测试性能都是用的这两个例子,而我主要介绍 helloworld basicfwd这两个例子 和 testpmd的结合,编写一个发包的dpdk应用程序,主要还是分享开发流程。

3.helloworld

在这里插入图片描述 这个代码并不复杂,我们主要关注四个地方: rte_eal_init(); 这个函数初始化dpdk EAL环境抽象层,这是所有的dpdk应用程序都会使用到的通用模块,想要具体了解的话可以阅读guides文档,在开发过程中一般是首先需要被调用的。

RTE_LCORE_FOREACH_SLAVE(); 这个宏应该比较好理解,就是一个for循环,遍历所有的slave核(只有一个master核,其余是slave核,比如一个4核cpu,0核为master核,1、2、3为slave核)。

rte_eal_remote_launch(); 这个函数功能也很简单,真要类比的话类似于多线程或者多进程的函数,它相当于另开了一个线程或者进程去执行lcore_hello(自定义的函数),只不过它使用另一个核去完成。第二个参数是传给自定义函数的参数。

除了rte_eal_remote_launch(),还有一个rte_eal_mp_remote_launch(),这个函数不需要遍历,直接会调用所有的核去执行我们自定义的函数。

rte_eal_mp_wait_lcore(); 看到wait就应该心里有数了,这个是在等待所有核core结束。这里依然有一个rte_eal_wait_lcore(…,core_id)函数,等待对应核结束,实际上rte_eal_mp_wait_lcore内部应该是让每一个核执行rte_eal_wait_lcore()来实现的。

4.以skeleton(basicfwd)为基础,编写自己的应用程序。

通过对helloworld示例的介绍,最主要的是掌握了类似于多线程或者多进程的rte_eal_remote_launch(),下面是basicfwd的源码,它只使用了一个核,但是我们使用的时候,可以通过rte_eal_remote_launch()增加核来处理自己的业务。

/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2010-2015 Intel Corporation */ #include #include #include #include #include #include #include #define RX_RING_SIZE 1024 #define TX_RING_SIZE 1024 #define NUM_MBUFS 8191 #define MBUF_CACHE_SIZE 250 #define BURST_SIZE 32 static const struct rte_eth_conf port_conf_default = { .rxmode = { .max_rx_pkt_len = RTE_ETHER_MAX_LEN, }, }; /* basicfwd.c: Basic DPDK skeleton forwarding example. */ /* * Initializes a given port using global settings and with the RX buffers * coming from the mbuf_pool passed as a parameter. */ static inline int port_init(uint16_t port, struct rte_mempool *mbuf_pool) { struct rte_eth_conf port_conf = port_conf_default; const uint16_t rx_rings = 1, tx_rings = 1; uint16_t nb_rxd = RX_RING_SIZE; uint16_t nb_txd = TX_RING_SIZE; int retval; uint16_t q; struct rte_eth_dev_info dev_info; struct rte_eth_txconf txconf; if (!rte_eth_dev_is_valid_port(port)) return -1; retval = rte_eth_dev_info_get(port, &dev_info); if (retval != 0) { printf("Error during getting device (port %u) info: %s\n", port, strerror(-retval)); return retval; } if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MBUF_FAST_FREE; /* Configure the Ethernet device. */ retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf); if (retval != 0) return retval; retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd); if (retval != 0) return retval; /* Allocate and set up 1 RX queue per Ethernet port. */ for (q = 0; q 2, etc. */ RTE_ETH_FOREACH_DEV(port) { /* Get burst of RX packets, from first port of pair. */ struct rte_mbuf *bufs[BURST_SIZE]; const uint16_t nb_rx = rte_eth_rx_burst(port, 0, bufs, BURST_SIZE); if (unlikely(nb_rx == 0)) continue; /* Send burst of TX packets, to second port of pair. */ const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0, bufs, nb_rx); /* Free any unsent packets. */ if (unlikely(nb_tx 0 && rte_eth_dev_socket_id(port) != (int)rte_socket_id()) printf("WARNING, port %u is on remote NUMA node to " "polling thread.\n\tPerformance will " "not be optimal.\n", port); printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n", rte_lcore_id()); d_addr.addr_bytes[0] = 88; d_addr.addr_bytes[1] = 83; d_addr.addr_bytes[2] = 192; d_addr.addr_bytes[3] = 57; d_addr.addr_bytes[4] = 0; d_addr.addr_bytes[5] = 58; s_addr.addr_bytes[0] = 88; s_addr.addr_bytes[1] = 83; s_addr.addr_bytes[2] = 192; s_addr.addr_bytes[3] = 57; s_addr.addr_bytes[4] = 0; s_addr.addr_bytes[5] = 220; rte_ether_addr_copy(&d_addr, ð_hdr.d_addr); rte_ether_addr_copy(&s_addr, ð_hdr.s_addr); eth_hdr.ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4); for (;;) { // RTE_ETH_FOREACH_DEV(port) { // struct rte_mbuf *bufs[BURST_SIZE]; // const uint16_t nb_rx = rte_eth_rx_burst(port, 0, // bufs, BURST_SIZE); // if (unlikely(nb_rx == 0)) // continue; // const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0, // bufs, nb_rx); // if (unlikely(nb_tx < nb_rx)) { // uint16_t buf; // for (buf = nb_tx; buf < nb_rx; buf++) // rte_pktmbuf_free(bufs[buf]); // } // } /* **pkt_burst_prepare */ if (rte_mempool_get_bulk(tx_mbuf_pool, (void **)pkts_burst, nb_pkt_per_burst) == 0) { for (nb_pkt = 0; nb_pkt


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3